from PySide6.QtGui import QStandardItemModel, QStandardItem

class DictTableViewModel(QStandardItemModel):
    """将字典数据转换为表格模型的类"""
    
    def __init__(self):
        super().__init__()
        self.setHorizontalHeaderLabels(["键", "值", "类型"])
    
    def populate(self, data, parent_key=""):
        """递归填充表格模型"""
        if isinstance(data, dict):
            for key, value in data.items():
                full_key = f"{parent_key}.{key}" if parent_key else key
                if isinstance(value, (dict, list)):
                    # 对于嵌套结构，添加一个条目显示类型和大小
                    value_text = ""
                    if isinstance(value, dict):
                        value_text = f"字典 ({len(value)} 项)"
                    elif isinstance(value, list):
                        value_text = f"列表 ({len(value)} 项)"
                    
                    item_key = QStandardItem(full_key)
                    item_value = QStandardItem(value_text)
                    item_type = QStandardItem(type(value).__name__)
                    self.appendRow([item_key, item_value, item_type])
                    
                    # 递归处理嵌套数据
                    if isinstance(value, dict):
                        self.populate(value, full_key)
                    elif isinstance(value, list):
                        self._populate_list(value, full_key)
                else:
                    # 基本数据类型直接显示
                    item_key = QStandardItem(full_key)
                    item_value = QStandardItem(str(value))
                    item_type = QStandardItem(type(value).__name__)
                    self.appendRow([item_key, item_value, item_type])
        elif isinstance(data, list):
            self._populate_list(data, parent_key)
    
    def _populate_list(self, data, parent_key):
        """处理列表数据"""
        for i, value in enumerate(data):
            full_key = f"{parent_key}[{i}]"
            if isinstance(value, (dict, list)):
                # 对于嵌套结构，添加一个条目显示类型和大小
                value_text = ""
                if isinstance(value, dict):
                    value_text = f"字典 ({len(value)} 项)"
                elif isinstance(value, list):
                    value_text = f"列表 ({len(value)} 项)"
                
                item_key = QStandardItem(full_key)
                item_value = QStandardItem(value_text)
                item_type = QStandardItem(type(value).__name__)
                self.appendRow([item_key, item_value, item_type])
                
                # 递归处理嵌套数据
                if isinstance(value, dict):
                    self.populate(value, full_key)
                elif isinstance(value, list):
                    self._populate_list(value, full_key)
            else:
                # 基本数据类型直接显示
                item_key = QStandardItem(full_key)
                item_value = QStandardItem(str(value))
                item_type = QStandardItem(type(value).__name__)
                self.appendRow([item_key, item_value, item_type])
